home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-06-28 | 7.3 KB | 353 lines | [TEXT/CWIE] |
- // Window.cp
-
- #ifndef Window_h
- #include "Window.h"
- #endif
- #ifndef WindowDefinition_h
- #include "WindowDefinition.h"
- #endif
- #ifndef WindowUpdater_h
- #include "WindowUpdater.h"
- #endif
- #ifndef MouseDownEvent_h
- #include "MouseDownEvent.h"
- #endif
- #ifndef View_h
- #include "View.h"
- #endif
- #ifndef TangibleView_h
- #include "TangibleView.h"
- #endif
- #ifndef ContextMaintainer_h
- #include "ContextMaintainer.h"
- #endif
- #ifndef GraphicsDeviceObject_h
- #include "GraphicsDeviceObject.h"
- #endif
- #ifndef MenuBar_h
- #include "MenuBar.h"
- #endif
- #ifndef WindowInitializer_h
- #include "WindowInitializer.h"
- #endif
- #ifndef Application_h
- #include "Application.h"
- #endif
- #ifndef BroadcastLoop_h
- #include "BroadcastLoop.h"
- #endif
- #ifndef WindowPile_h
- #include "WindowPile.h"
- #endif
- #ifndef WindowLocation_h
- #include "WindowLocation.h"
- #endif
- #ifndef ViewMap_h
- #include "ViewMap.h"
- #endif
-
- Window::Window( const WindowDefinition& theDefinition )
- : WindowController( visibility, zoomed, bounds, name, index ),
- Receiver<PreparingToQuit>( Application::The() ),
- definition( theDefinition ),
- window( theDefinition.Procedure(), theDefinition.Closeable() ),
- pane( window ),
- context( &window.port ),
- registration( &window.port, this ),
- closingLink( focus, *this ),
- visibility( *this, &Window::GetVisibility, &Window::SetVisibility ),
- zoomed( *this, &Window::GetZoomed, &Window::SetZoomed ),
- bounds( *this, &Window::GetBounds, &Window::SetBounds ),
- name( *this, &Window::GetName, &Window::SetName ),
- index( *this, &Window::GetIndex, &Window::SetIndex )
- {
- WindowLocator::Register( registration );
- }
-
- Window::~Window()
- {
- }
-
- void Window::Initialize( const WindowInitializer& initializer )
- {
- SetName( initializer.NameFor( *this ) );
- SetBounds( initializer.PositionFor( *this ) );
- if ( initializer.VisibilityFor( *this ) )
- SetIndex( initializer.IndexFor( *this ) );
- }
-
- void Window::Update()
- {
- ContextMaintainer cm( &context );
-
- WindowUpdater updater( &window.port );
-
- View& view( *pane );
- view.SetContext();
- ViewMap map( view );
-
- if ( map.Visible() )
- view.Draw( map );
- }
-
- void Window::Activate()
- {
- focus.BeFavored();
- Assert( focus.Active() );
- }
-
- void Window::Deactivate()
- {
- focus.BeDisfavored();
- Assert( !focus.Active() );
- }
-
- void Window::ClickContent( const MouseDownEvent& event )
- {
- ContextMaintainer cm( &context );
-
- PointObject hit( event.Where() );
- hit.GlobalToLocal();
-
- TangibleView *view = pane->Touch( hit );
- if ( view != 0 && ( !event.Activating() || view->WantsActivatingClick() ) )
- {
- view->SetContext();
- view->Click( hit, event );
- }
-
- if ( FrontWindow() != &window.port )
- index.Set( 1 );
- }
-
- void Window::ClickDrag( const MouseDownEvent& event )
- {
- if ( event.Activating() && !event.Command() )
- index.Set( 1 );
-
- window.MoveByDragging( event.Where() );
- bounds = bounds; // To help with recording
- }
-
- void Window::ClickClose( const MouseDownEvent& event )
- {
- if ( window.TrackCloseBox( event.Where() ) )
- Close();
- }
-
- void Window::ClickZoomIn( const MouseDownEvent& event )
- {
- if ( window.TrackZoomInBox( event.Where() ) )
- zoomed.Set( !GetZoomed() );
- }
-
- void Window::ClickZoomOut( const MouseDownEvent& event )
- {
- if ( window.TrackZoomOutBox( event.Where() ) )
- zoomed.Set( !GetZoomed() );
- }
-
- void Window::ClickGrow( const MouseDownEvent& event )
- {
- PointObject newSize = window.SizeByDragging( event.Where(),
- pane->MinimumSize(),
- pane->MaximumSize() );
-
- Rectangle newBounds( window.GlobalBounds() );
- newBounds.SetBottomRight( newBounds.TopLeft() + newSize );
-
- bounds.Set( newBounds );
- }
-
- bool Window::GetVisibility() const
- {
- return window.Visible();
- }
-
- void Window::SetVisibility( bool toShow )
- {
- if ( toShow )
- window.Show();
- else
- window.Hide();
- }
-
- bool Window::GetZoomed() const
- {
- return window.GlobalBounds() == BestBounds();
- }
-
- void Window::SetZoomed( bool toZoom )
- {
- if ( toZoom )
- {
- window.SetStandardBounds( BestBounds() );
- window.ZoomToStandardBounds();
- }
- else
- window.ZoomToUserBounds();
-
- pane.UpdateBounds();
- }
-
- Rectangle Window::GetBounds() const
- {
- return window.GlobalBounds();
- }
-
- void Window::SetBounds( Rectangle newBounds )
- {
- window.SetBounds( newBounds );
- pane.UpdateBounds();
- }
-
- ConstPString Window::GetName() const
- {
- return nameString;
- }
-
- void Window::SetName( ConstPString newName )
- {
- nameString = newName;
- window.SetTitle( newName );
- }
-
- uint32 Window::GetIndex() const
- {
- return window.Index();
- }
-
- void Window::SetIndex( uint32 newIndex )
- {
- window.SetIndex( newIndex );
- }
-
- GDHandle Window::DefaultScreen() const
- {
- WindowObject *front = WindowObject::Front();
- return front == 0
- ? GraphicsDeviceObject::Main()
- : front->NearestScreen();
- }
-
- Rectangle Window::DefaultPosition( GDHandle screen ) const
- {
- Rectangle available( AvailableArea( screen ) );
- PointObject bestSize = pane->BestSize( available.Size() );
- PointObject reasonableSize = pane->ReasonableSize();
-
- Rectangle bestPile;
- uint32 bestPileScore = maxuint32;
-
- for ( WindowPile pile( available, bestSize );
- pile.Unfinished() && bestPileScore > 0;
- pile++ )
- {
- uint32 score = WindowObject::CountVisibleWindows( *pile );
-
- if ( score >= bestPileScore )
- continue;
-
- bestPileScore = score;
- bestPile = *pile;
- }
-
- PointObject bestLocation;
- uint32 bestLocationScore = maxuint32;
-
- for ( WindowLocation location( bestPile, reasonableSize );
- location.Unfinished() && bestLocationScore > 0;
- location++ )
- {
- Rectangle nearby( *location, *location );
- nearby.Outset( 5 );
- uint32 score = WindowObject::CountVisibleWindows( nearby );
-
- if ( score >= bestLocationScore )
- continue;
-
- bestLocationScore = score;
- bestLocation = *location;
- }
-
- bestSize = pane->BestSize( available.BottomRight() - bestLocation );
-
- return Rectangle( bestLocation, bestLocation + bestSize );
- }
-
- bool Window::CanClose() const
- {
- return definition.Closeable();
- }
-
- void Window::Close( SavingOption savingOption )
- {
- for ( BroadcastLoop<PreparingToClose> receiver( *this );
- receiver.Unfinished();
- receiver++ )
- receiver->PrepareToClose( savingOption );
-
- window.Hide();
- focus.BeDisfavored();
- delete this;
- }
-
- void Window::PrepareToQuit( SavingOption savingOption )
- {
- Close( savingOption );
- }
-
- GDHandle Window::BestScreen() const
- {
- return window.NearestScreen();
- }
-
- Rectangle Window::BestBounds() const
- {
- return BestBounds( BestScreen() );
- }
-
- Rectangle Window::BestBounds( GDHandle screen ) const
- {
- return BestBounds( AvailableArea( screen ) );
- }
-
- Rectangle Window::BestBounds( Rectangle available ) const
- {
- Rectangle best( window.GlobalBounds() );
-
- best.SetBottomRight( best.TopLeft() + pane->BestSize( available.Size() ) );
-
- if ( best.right > available.right )
- best += PointObject( available.right - best.right, 0 );
-
- if ( best.bottom > available.bottom )
- best += PointObject( 0, available.bottom - best.bottom );
-
- if ( best.left < available.left )
- best += PointObject( available.left - best.left, 0 );
-
- if ( best.top < available.top )
- best += PointObject( 0, available.top - best.top );
-
- return best;
- }
-
- Rectangle Window::AvailableArea( GDHandle theScreen ) const
- {
- GraphicsDeviceObject screen( theScreen );
-
- Rectangle area( screen.Bounds() );
-
- if ( screen.IsMainScreen() )
- area.top += MenuBar::The().Height();
-
- area.Inset( 3 );
-
- area.Inset( definition.FrameSize() );
-
- return area;
- }
-
- #include "BroadcastLoop.cp"
-